package com.nineclock.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.document.dto.DocUserCollaborationDTO;
import com.nineclock.document.dto.DocumentUserDTO;
import com.nineclock.document.mapper.DocCollaborationsMapper;
import com.nineclock.document.mapper.DocFileMapper;
import com.nineclock.document.pojo.DocCollaborations;
import com.nineclock.document.pojo.DocFile;
import com.nineclock.document.service.DocCollaborationsService;
import com.nineclock.system.feign.SysCompanyUserFeign;
import com.nineclock.system.dto.SysCompanyUserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


@Service
@Transactional
public class DocCollaborationsServiceImpl implements DocCollaborationsService {

    @Autowired
    private DocCollaborationsMapper collaborationsMapper;

    @Autowired
    private SysCompanyUserFeign companyUserFeign;

    @Autowired
    private DocFileMapper fileMapper;

    @Override
    public PageResult<DocumentUserDTO> pagingCollaborations(Long id, Integer page, Integer pageSize) {

        if(id == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        

        // 创建分页对象，指定分页参数，参数1-当前页，参数2-页大小
        IPage<DocCollaborations> ipage = new Page<>(page, pageSize);
        LambdaQueryWrapper<DocCollaborations> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocCollaborations::getFileId, id);
        ipage = collaborationsMapper.selectPage(ipage, wrapper);

        // 获取当前页数据
        List<DocCollaborations> collaborationsList = ipage.getRecords();

        if(!CollectionUtils.isEmpty(collaborationsList)) {

            // 流操作，将 List<DocCollaborations> --> List<DocumentUserDTO>
            List<DocumentUserDTO> documentUserDTOS = collaborationsList.stream().map(docCollaborations -> {

                DocumentUserDTO documentUserDTO = new DocumentUserDTO();
                documentUserDTO.setId(docCollaborations.getCollaborationId()); // 协作者id

                SysCompanyUserDTO companyUserDTO = companyUserFeign.queryCompanyUserById(documentUserDTO.getId()).getData();

                if (companyUserDTO != null) {
                    documentUserDTO.setUserName(companyUserDTO.getUserName());
                }
                return documentUserDTO;

            }).collect(Collectors.toList());

            return new PageResult<>(ipage.getTotal(), ipage.getPages(), documentUserDTOS);
        }

        return new PageResult<>(ipage.getTotal(), ipage.getPages(), null);
    }

    @Override
    public List<DocUserCollaborationDTO> pagingUsers(Long id) {
        // 参数校验
        if(id == null) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        // 获取当前企业所有员工列表
        List<SysCompanyUserDTO> companyUserDTOList = companyUserFeign.queryAllCompanyUser().getData();

        // 根据文档id，查询协作者信息列表
        LambdaQueryWrapper<DocCollaborations> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DocCollaborations::getFileId, id);
        List<DocCollaborations> collaborationsList = collaborationsMapper.selectList(wrapper);


        // 得到所有协作者id集合
        List<Long> idList = collaborationsList.stream().map(docCollaborations -> {
            return docCollaborations.getCollaborationId();
        }).collect(Collectors.toList());

        // 根据文档id查询文档内容
        DocFile docFile = fileMapper.selectById(id);

        List<DocUserCollaborationDTO> collaborationDTOList = companyUserDTOList.stream().map(sysCompanyUserDTO -> {
            DocUserCollaborationDTO userCollaborationDTO = new DocUserCollaborationDTO();
            userCollaborationDTO.setId(sysCompanyUserDTO.getId());
            userCollaborationDTO.setUsername(sysCompanyUserDTO.getUserName());
            userCollaborationDTO.setImgUrl(sysCompanyUserDTO.getImageUrl());
            userCollaborationDTO.setPhone(sysCompanyUserDTO.getMobile());

            if (sysCompanyUserDTO.getId() == docFile.getCreateUserId()) {

                userCollaborationDTO.setState(1);

            } else if (idList.contains(sysCompanyUserDTO.getId())) {

                userCollaborationDTO.setState(2);

            } else { // 既不是协作者，也不是拥有者

                userCollaborationDTO.setState(0);

            }
            return userCollaborationDTO;

        }).collect(Collectors.toList());

        return collaborationDTOList;
    }
}
